import 'package:dio/dio.dart';
import 'dart:async';

import '../config/service_url.dart';
import '../model/diary_entity.dart';
import '../model/entity_factory.dart';
import '../model/msg_entity.dart';

class DiaryListDao {
  static Future<DiaryListEntity?> fetch(DateTime ts,String url) async {
    try {
      Map<String, dynamic> map = {"ts": ts};
      Response response = await Dio()
          .get(servicePath[url]!, queryParameters: map);
      if (response.statusCode == 200 && response.data['code'] == 200) {
        return EntityFactory.generateOBJ<DiaryListEntity>(response.data);
      } else {
        throw Exception("StatusCode: ${response.statusCode}");
      }
    } catch (e) {
      return null;
    }
  }
}

class DiaryDetailDao {
  static Future<DiaryEntity?> ?fetch(int? diaryId) async {
    try {
      Response response =
          await Dio().get(servicePath['getDiaryDetail']! + diaryId.toString());
      if (response.statusCode == 200 && response.data['code'] == 200) {
        return EntityFactory.generateOBJ<DiaryEntity>(response.data);
      } else {
        throw Exception("StatusCode: ${response.statusCode}");
      }
    } catch (e) {
      return null;
    }
  }
}

class DiarySaveOrUpdateDao {
  static Future<MsgEntity?> fetch(Map<String, dynamic> map) async {
    try {
      Response response =
          await Dio().put(servicePath['saveOrUpdateDiary']!, data: map);
      return EntityFactory.generateOBJ<MsgEntity>(response.data);
    } catch (e) {
      return null;
    }
  }
}
